package com.laopan.wxpay.paycallback;

import com.laopan.wxpay.bean.refund.RefundCallbackResult;
import com.laopan.wxpay.bean.result.OrderResult;
import com.laopan.wxpay.enums.CallBackEventType;
import com.laopan.wxpay.exception.WxPayException;
import com.laopan.wxpay.utils.JsonUtils;

/**
 * <p>
 *
 * </p>
 *
 * @author laopan
 * @className DefaultPayCallbackExecProcessor
 * @date 2021/4/29 11:34
 */

public class DefaultPayCallbackExecProcessor implements PayCallbackExecProcessor {

    private  PayCallbackFactory payCallbackFactory;

    public DefaultPayCallbackExecProcessor(PayCallbackFactory payCallbackFactory) {
        this.payCallbackFactory = payCallbackFactory;
    }
    @Override
    public boolean processor(String execEventType, String data) {
        if (CallBackEventType.TRANSACTION_SUCCESS.getCode().equals(execEventType)){
            OrderResult orderResult= JsonUtils.fromJson(data,OrderResult.class);
            return payCallbackFactory.callback(orderResult);
        }else if (CallBackEventType.REFUND_SUCCESS.getCode().equals(execEventType)){
            RefundCallbackResult refundResult=JsonUtils.fromJson(data,RefundCallbackResult.class);
            return payCallbackFactory.refundCallback(refundResult);
        }else{
          throw  new WxPayException("无效的回调数据类型");
        }
    }
}
